package com.ihighest.app.infox.adapter;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.activity.ProductInforActivity;

public class ColumMainProductImgAdapter extends BaseAdapter {
	private final Context context;
	private JSONArray arraylist;
	private final LayoutInflater inflater;

	public ColumMainProductImgAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
	}

	public ColumMainProductImgAdapter(Context context, JSONArray list) {
		this.context = context;
		this.arraylist = list;
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public int getCount() {
		if (arraylist == null) {
			return 0;
		}

		return arraylist.length();
	}

	@Override
	public Object getItem(int arg0) {
		try {
			return arraylist.getJSONObject(arg0);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public View getView(int posiotion, View contentview, ViewGroup parent) {
		Viewholder holder;
		if (contentview == null) {
			holder = new Viewholder();
			contentview = inflater.inflate(R.layout.infox_activity_companypage_imgitem, null);
			holder.img = (ImageView) contentview.findViewById(R.id.MianProductItemIng);
			holder.text = (TextView) contentview.findViewById(R.id.MianProductItemName);
			contentview.setTag(holder);
		} else {
			holder = (Viewholder) contentview.getTag();
		}

		try {
			String imgurl = arraylist.getJSONObject(posiotion).getString("image");
			String title = arraylist.getJSONObject(posiotion).getString("title");
			final String id = arraylist.getJSONObject(posiotion).getString("id");// 产品id
			holder.text.setText(title);
			// DisplayImageOptions defaultOptions = new
			// DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.infox_person_no_icon)
			// .showImageOnFail(R.drawable.infox_person_no_icon).showImageOnLoading(R.drawable.infox_person_no_icon)
			// .cacheInMemory(true).cacheOnDisc(true).build();
			// ImageLoader.getInstance().displayImage(imgurl, holder.img,
			// defaultOptions);
			AppKit.SetNormalImg(imgurl, holder.img);
			contentview.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {

					Intent intent = new Intent(context, ProductInforActivity.class);
					intent.putExtra("id", id);
					context.startActivity(intent);

				}
			});

		} catch (JSONException e) {
			e.printStackTrace();
		}

		return contentview;
	}

	class Viewholder {
		ImageView img;
		TextView text;
	}
}
